package com.restaurant.application.start;

import com.nebutown.commonunits.alarm.alarmlist.mysql.AlarmListImpl;
import com.nebutown.entry.EntryServlet;
import com.nebutown.entry.SimpleFileServlet;
import com.nebutown.entry.UnitAdapterServlet;
import com.nebutown.facilities.F;
import com.nebutown.facilities.NBSession;
import com.nebutown.facilities.SeedUpdater;
import com.nebutown.generation.IDGenerator;
import com.restaurant.application.indexes.accumulategoodslist.mysql.AccumulateGoodsListImpl;
import com.restaurant.application.indexes.accumulatelist.mysql.AccumulateListTableImpl;
import com.restaurant.application.indexes.bookingsetlist.mysql.BookingSetListImpl;
import com.restaurant.application.indexes.consumptionlist.mysql.ConsumptionListImpl;
import com.restaurant.application.indexes.couponlist.mysql.CouponListImpl;
import com.restaurant.application.indexes.couponmanagelist.mysql.CouponManageListImpl;
import com.restaurant.application.indexes.customerlist.mysql.CustomerListImpl;
import com.restaurant.application.indexes.exchangegoodslist.mysql.ExchangeGoodsListImpl;
import com.restaurant.application.indexes.genshanguserList.mysql.GenShangUserListImpl;
import com.restaurant.application.indexes.reservationlist.mysql.ReservationListImpl;
import com.restaurant.application.indexes.signinlist.mysql.SignInListImpl;
import com.restaurant.application.root.Root;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.MultipartConfigElement;

public class JettyServer {
    private static final Logger logger = LoggerFactory.getLogger(JettyServer.class);

    public static void main(String[] args) {

        logger.debug("Starting jetty server");

        IDGenerator.setSeedUpdater(new SeedUpdater());

        F.getStation().startBD();

        F.getIndexSummary()
                .with(CustomerListImpl.class)
                .with(CouponListImpl.class)
                .with(CouponManageListImpl.class)
                .with(ReservationListImpl.class)
                .with(ConsumptionListImpl.class)
                .with(AccumulateGoodsListImpl.class)
                .with(SignInListImpl.class)
                .with(AccumulateGoodsListImpl.class)
                .with(ExchangeGoodsListImpl.class)
                .with(AccumulateListTableImpl.class)
                .with(BookingSetListImpl.class)
                .with(AlarmListImpl.class)
                .with(GenShangUserListImpl.class)
        ;
        F.getIndexSummary().runInit();


        //AlarmRoot.addAlarmClock(AlarmClock.class);
        //AlarmRoot.start();


        NBSession.setRootUnitType(Root.class.getName());

        ServletContextHandler handler = new ServletContextHandler();

        handler.addServlet(EntryServlet.class.getName(), "/Gesture");

        handler.addServlet(SimpleFileServlet.class.getName(), "/Download");
        handler.addServlet(SimpleFileServlet.class.getName(), "/Direct/*");
        handler.addServlet(ImageToken.class.getName(), "/ImageUpload");

        ServletHolder holder = new ServletHolder(UnitAdapterServlet.class);
        holder.getRegistration().setMultipartConfig(new MultipartConfigElement("/tmp"));
        handler.addServlet(holder, "/Adapter/*");

        F.getStation().startMachine(handler);
    }
}
